﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using ServiceStack;

namespace em
{
    class Program
    {
        public static void Rep(string s)
        {
            ReplaceColor(s, Color.FromArgb(255, 166, 166), Color.White, 84);
            ReplaceColor(s, Color.FromArgb(0xdd, 0xdd, 0xdd), Color.White, 84);
            ReplaceColor(s, Color.FromArgb(0, 0, 255), Color.Black, 84);
            ReplaceColor(s, Color.FromArgb(0x40, 0x70, 0x3e), Color.Black, 0x80);
            FindPicReplaceIt(s, "C:\\Users\\ACER\\Desktop\\repos\\sim\\3.bmp", 0.07f, TemplateMatchingType.Ccoeff);
            FindPicReplaceBestIt(s, "C:\\Users\\ACER\\Desktop\\repos\\sim\\1.bmp", new Size(150, 16));

            Console.WriteLine(s + "//");

        }

        static void Work523524522()
        {
            foreach (var s in Directory.GetFiles("C:\\Users\\ACER\\Desktop\\522"))
            {
                Rep(s);

            }
            foreach (var s in Directory.GetFiles("C:\\Users\\ACER\\Desktop\\523"))
            {
                Rep(s);

            }
            foreach (var s in Directory.GetFiles("C:\\Users\\ACER\\Desktop\\524"))
            {
                Rep(s);

            }

            Console.WriteLine("ok");
            Console.Read();
        }
        static void Main(string[] args)
        {
            var files = Directory.GetFiles("C:\\Users\\yy\\Desktop\\524");
            foreach (string file in files)
            {
                Mat m =new Mat(file);
                CvInvoke.CopyMakeBorder(m,m,0,0,0,930-m.Width,BorderType.Constant,new MCvScalar(255,255,255));
                m.Save(file);
            }
        




        }

        public static void wzsbORCs()
        {
//            Dm.dmsoftClass dm = new dmsoftClass();
//            dm.SetDict(0, "F:\\dll\\Dropbox\\tools\\dm_soft.txt");
//            var files = Directory.GetFiles("C:\\Users\\ACER\\Desktop\\524\\d");
//            List<MyClass> ms = new List<MyClass>();
//            for (var i = 0; i < files.Length; i++)
//            {
//                string file = files[i];
//                string a = dm.OcrInFile(0, 0, 200, 25, file, "000000-333333", 0.9);
//                ms.Add(new MyClass() { id = i + 1, answer = a });
//                Console.WriteLine($"{i + 1}:{a}");
//            }
//            File.WriteAllText("a.csv", ms.ToCsv(), Encoding.Default);
//            Console.WriteLine("ok");
//            Console.Read();
        }
        public class MyClass
        {
            public int id { get; set; }
            public string answer { get; set; }
        }

        public static void crop(string srcfile,Rectangle rect,string newfile)
        {
            Mat src = new Mat(srcfile);
            Mat cr = new Mat(src, rect);
            cr.Save(newfile);
        }

        public static void para()
        {
            Parallel.ForEach(Directory.GetFiles("C:\\Users\\ACER\\Desktop\\522"), (Rep));

            Parallel.ForEach(Directory.GetFiles("C:\\Users\\ACER\\Desktop\\523"), (Rep));
            Parallel.ForEach(Directory.GetFiles("C:\\Users\\ACER\\Desktop\\524"), (Rep));
            Console.WriteLine("ok");
            Console.Read();
        }
        public static void ReplaceColor(string file, Color oldColor, Color newColor, int rc, string newfile = null)
        {
            Image<Bgr, byte> par = new Image<Bgr, byte>(file);
            Bgr newBgr = new Bgr(newColor.B, newColor.G, newColor.R);
            for (int i = 0; i < par.Width; i++)
            {
                for (int j = 0; j < par.Height; j++)
                {
                    Bgr bgr = par[j, i];
                    if (Math.Abs(bgr.Red - oldColor.R) < rc && Math.Abs(bgr.Green - oldColor.G) < rc &&
                        Math.Abs(bgr.Blue - oldColor.B) < rc)
                    {
                        par.Draw(new[] { new Point(i, j), new Point(i, j) }, newBgr, -1);
                    }
                }
            }
            par.Save(newfile ?? file);
        }

        public static void FindPicReplaceIt(string parfile, string subfile, float intensity, TemplateMatchingType tmp,
            Size rec = new Size(), string newfile = null)
        {
            Image<Bgr, byte> sub = new Image<Bgr, byte>(subfile);
            Image<Bgr, byte> par = new Image<Bgr, byte>(parfile);
            Image<Gray, float> c = par.MatchTemplate(sub, TemplateMatchingType.Ccoeff);
            CvInvoke.Normalize(c, c);
            List<Point> ps = new List<Point>();
            for (int i = 0; i < c.Size.Width; i++)
            {
                for (int j = 0; j < c.Size.Height; j++)
                {
                    var cc = c[j, i];
                    if (cc.Intensity > intensity)
                    {
                        Console.WriteLine(parfile + " ** " + cc.Intensity + " x:" + i + " y:" + j);
                        ps.Add(new Point(i, j));
                    }
                }
            }
            if (rec.Width == 0 && rec.Height == 0)
            {
                foreach (var p in ps)
                {
                    par.Draw(new Rectangle(p.X, p.Y, sub.Width, sub.Height), new Bgr(Color.White), -1);
                }
            }
            else
            {
                foreach (var p in ps)
                {
                    par.Draw(new Rectangle(p.X, p.Y, rec.Width, rec.Height), new Bgr(Color.White), -1);
                }

            }
            par.Save(newfile ?? parfile);
        }
        public static void FindPicReplaceBestIt(string parfile, string subfile,Size rec = new Size(), string newfile = null)
        {
            Image<Bgr, byte> sub = new Image<Bgr, byte>(subfile);
            Image<Bgr, byte> par = new Image<Bgr, byte>(parfile);
            Image<Gray, float> c = par.MatchTemplate(sub, TemplateMatchingType.Ccoeff);
            c.MinMax(out var minValues,out var maxValues,out var minLocations,out var maxLocations);
            CvInvoke.Normalize(c, c);

            if (rec.Width == 0 && rec.Height == 0)
            {
                foreach (var p in maxLocations)
                {
                    int w=par.Width-p.X;
                    int h= par.Height - p.Y ;
                    Rectangle rect = new Rectangle(p.X, p.Y, w,h);
                    crop(parfile,rect,Path.Combine(Path.GetDirectoryName(parfile),Path.GetFileNameWithoutExtension(parfile))+"-d.jpg");
                    par.Draw(rect, new Bgr(Color.White), -1);
                    Console.WriteLine(parfile + " ** " + maxValues[0] + " x:" + p.X + " y:" + p.Y);

                }
            }
            else
            {
                foreach (var p in maxLocations)
                {
                    int w = par.Width - p.X;
                    int h = par.Height - p.Y;
                    Rectangle rect = new Rectangle(p.X, p.Y,w, h);
                    crop(parfile, rect, Path.Combine(Path.GetDirectoryName(parfile), Path.GetFileNameWithoutExtension(parfile)) + "-d.jpg");
                    par.Draw(rect, new Bgr(Color.White), -1);
                    Console.WriteLine(parfile + " ** " + maxValues[0] + " x:" + p.X + " y:" + p.Y);

                }

            }
            par.Save(newfile ?? parfile);
        }

    }
}
